package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.educenter.vo.RegisterVo;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Cyrus
 * @since 2020-12-13
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 登陆
     */
    @Override
    public String login(UcenterMember member) {
        //获取登陆手机号、密码
        String mobile = member.getMobile();
        String password = member.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "账号或密码不能为空");
        }
        //通过手机号进行查询
        UcenterMember ucenterMember = baseMapper.selectOne(new QueryWrapper<UcenterMember>().lambda().eq(UcenterMember::getMobile, mobile));
        if (ucenterMember == null) {
            throw new GuliException(20001, "当前手机号不存在");
        }
        //判断密码是否正确
        if (!MD5.encrypt(password).equals(ucenterMember.getPassword())) {
            throw new GuliException(20001, "输入的密码错误");
        }
        //判断用户是否禁用
        if (ucenterMember.getIsDisabled()) {
            throw new GuliException(20001, "当前账号已经禁用");
        }

        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return token;
    }

    /**
     * 注册
     */
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册数据
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickName = registerVo.getNickName();
        String password = registerVo.getPassword();
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickName) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "注册失败，必填项不能为空");
        }
        //判断验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw new GuliException(20001, "验证码输入错误");
        }
        //判断手机号是否重复
        Integer count = baseMapper.selectCount(new QueryWrapper<UcenterMember>().lambda().eq(UcenterMember::getMobile, mobile));
        if (count > 0) {
            throw new GuliException(20001, "当前手机号已经注册");
        }

        //数据添加到数据库中
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickName);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://edu-school-cyrus.oss-cn-beijing.aliyuncs.com/2020/12/10/8c6b3eb49cc949dab20e9a2e88be4d5efile.png");
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenId(String openid) {
        return baseMapper.selectOne(new QueryWrapper<UcenterMember>().lambda().eq(UcenterMember::getOpenid, openid));



    }

    @Override
    public int getRegisterCount(String day) {

        int count = baseMapper.getRegisterCount(day);

        return count;
    }


}
